/*
 * Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 * This file is part of e3pool.
 *
 * e3pool is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * e3pool is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with e3pool. If not, see <https://www.gnu.org/licenses/>.
 */

package go_pool

type GoSyncFunc = func(...any) (any, error)

type GoAsyncFunc = func(...any)

type BlockHandler interface {
	OnBlock(GoRunner)
}

type PanicHook interface {
	OnPanic(err error)
}

type GoRunner interface {
	SyncRun(GoSyncFunc, ...any) (any, error)
	AsyncRun(GoAsyncFunc, ...any) error
	Wait()
}

type GoWaitFunc interface {
	Run(where string, f GoAsyncFunc, args ...any)
	Wait()
}

// SetPoolSize 设置go_pool缓存大小
func SetPoolSize(cnt uint16) {
	setPoolSize(cnt)
}

func NewGoRunner(hook PanicHook, where string, opts ...*Options) GoRunner {
	var o = DefaultOptions()
	if opts != nil && opts[0] != nil {
		o = opts[0]
	}
	if o.simCount == 0 {
		return newSimRunner(hook, where)
	} else {
		return newCacheRunner(hook, where, *o)
	}
}

func NewGoWorkerDo(hook PanicHook, where string, f func(...any), args ...any) {
	goWorkerDo(func(as ...any) {
		safeFunc(hook, where, f, args...)
	})
}

func NewGoWaitFunc(hook PanicHook) GoWaitFunc {
	return &goWaitFunc{hand: hook}
}
